<?php

namespace app\merchant_staff\controller\billiard;

use addons\mycurrency\library\Common;
use app\common\controller\MerchantStaff;
use app\common\library\Ems;
use app\common\library\Sms;
use app\common\model\Area;
use app\common\model\billiard\Goods;
use app\common\model\mycurrency\LeaseStrategy;
use app\common\model\mycurrency\MerchantStore;
use fast\Random;
use think\Config;
use think\Db;
use think\Exception;
use think\Validate;
use app\common\model\mycurrency\MerchantStaff as StaffMoney;

/**
 * 门店商品
 */
class StoreGoods extends MerchantStaff
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

        if ($this->identity != 2){
            $this->error('非门店账号');
        }
    }

    /**
     * 产品列表
    */
    public function storeGoodsList(){
        $params = $this->request->param();
        $rule = [
            ['page', 'require', '页码不能为空'],
            ['pageSize', 'require', '每页查询条数不能为空'],
        ];
        $check = $this->validate($params, $rule);
        if ($check !== true) {
            $this->error($check);
        }
        $where = [
            'store_id' => $this->store_id,
            'deletetime' => null,
        ];
        $list = \app\common\model\billiard\StoreGoods::with(['strategy','goods'])
            ->where($where)
            ->limit(($params['page'] - 1) * $params['pageSize'], $params['pageSize'])
            ->order('id desc')
            ->field('*')
            ->select();
        foreach ($list as $k => $v){
            $list[$k]['goods']['abbreviation_image'] = Common::GetHost() . $list[$k]['goods']['abbreviation_image'];
        }
        $num = \app\common\model\billiard\StoreGoods::where($where)->count();
        if ($num > $params['page'] * $params['pageSize']) {
            $nextpage = true;
        } else {
            $nextpage = false;
        }
        $this->success('获取成功', ['list' => $list, 'nextpage' => $nextpage]);
    }

    /**
     * 添加产品 - 系统产品列表
     */
    public function GoodsList(){
        $data = Goods::where(['status' => Goods::STATUS_ZHENGCHENG,'deletetime' => null])->field('id,title')->select();
        $this->success('加载成功',$data);
    }

    /**
     * 添加产品
     */
    public function storeGoodsAdd(){
        $params = $this->request->param();
        $rule = [
            ['goods_id', 'require', '系统产品id不能为空'],
            ['advance_fee', 'require', '预付金不能为空'],
            ['duration', 'require', '收费标准时长不能为空'],
            ['unit_price', 'require', '收费标准价格不能为空'],
            //['overtime_money', 'require', '租赁超时收费金额不能为空'],
            //['overtime_company', 'require', '租赁超市收费单位不能为空'],
        ];
        $check = $this->validate($params, $rule);
        if ($check !== true) {
            $this->error($check);
        }
        $store = MerchantStore::get($this->store_id);
        $goods = Goods::where(['id' => $params['goods_id'],'deletetime' => null])->find();
        if(!$goods){
            $this->error('产品不存在');
        }
        if($goods['status'] == Goods::STATUS_JINYONG){
            $this->error('产品不可用');
        }
        if (\app\common\model\billiard\StoreGoods::where(['store_id' => $this->store_id,'goods_id' => $goods->id,'deletetime' => null])->find()){
            $this->error('产品已经存在请勿重复添加');
        }
        Db::startTrans();
        try {
            //添加计费策略
            $strategy_increase = [
                'title' => "{$store->store_name}-{$goods->title}【收费标准】",
                'types' => LeaseStrategy::TYPES_SHICHANG,
                'duration' => $params['duration'],
                'unit_price' => $params['unit_price'],
                'free_duration' => 0,
                'free_frequency' => 0,
                'free_cycle' => 0,
                'overtime_money' => 0,//$params['overtime_money'],
                'overtime_company' => 0,// $params['overtime_company'],
                'status' => LeaseStrategy::STATUS_ZHENGCHENG,
                'createtime' => time(),
            ];
            $strategy = LeaseStrategy::create($strategy_increase);

            $increase = [
                'store_id' => $this->store_id,
                'lease_strategy_id' => $strategy->id,
                'goods_id' => $goods->id,
                'advance_fee' => $params['advance_fee'],
                'createtime' => time(),
            ];
            \app\common\model\billiard\StoreGoods::create($increase);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('添加成功');
    }

    /**
     * 编辑产品
     */
    public function storeGoodsUpdate(){
        $params = $this->request->param();
        $rule = [
            ['advance_fee', 'require', '预付金不能为空'],
            ['duration', 'require', '收费标准时长不能为空'],
            ['unit_price', 'require', '收费标准价格不能为空'],

            ['store_goods_id', 'require', '门店产品id不能为空'],
        ];
        $check = $this->validate($params, $rule);
        if ($check !== true) {
            $this->error($check);
        }
        $store_goods = \app\common\model\billiard\StoreGoods::where(['store_id' => $this->store_id,'id' => $params['store_goods_id'],'deletetime' => null])->find();
        if (!$store_goods){
            $this->error('门店产品不存在');
        }
        Db::startTrans();
        try {
            //修改计费策略
            $strategy_update = [
                'duration' => $params['duration'],
                'unit_price' => $params['unit_price'],
                'updatetime' => time(),
            ];
            $strategy = LeaseStrategy::where(['id' => $store_goods->lease_strategy_id])->update($strategy_update);

            $store_goods->advance_fee = $params['advance_fee'];
            $store_goods->save();
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('修改成功');
    }

    /**
     * 删除产品
     */
    public function storeGoodsDel(){
        $params = $this->request->param();
        $rule = [
            ['store_goods_id', 'require', '门店产品id不能为空'],
        ];
        $check = $this->validate($params, $rule);
        if ($check !== true) {
            $this->error($check);
        }
        $store_goods = \app\common\model\billiard\StoreGoods::where(['store_id' => $this->store_id,'id' => $params['store_goods_id'],'deletetime' => null])->find();
        if (!$store_goods){
            $this->error('门店产品不存在');
        }
        $store_goods->deletetime = time();
        $store_goods->save();
        $this->success('删除成功');
    }



}
